#ifndef E_CONTROLLER_H
#define E_CONTROLLER_H

#include <DefineLibCommon.h>
#include <DefineCommon.h>
#include <memory>

#include <EItem.h>
#include <EItemVisitor.h>
#include <ECommand.h>


/**This class is designed based on the concept of MVC frmaework
 * EController translate datas and calling between UI and EItem(as Model)
 * There are two primary works for EController:
 * 1. As a ItemVisitor, traveling the tree structure.
 * 2. Handle ECommand.
 * */
class EController;
typedef SmartPtr(EController) EControllerPtr;
class COMMON_DLL EController : public EItemVisitor
{
public:
    EController();
    virtual ~EController();

    long getTag();

    void setRootItem(EItemPtr item);
    EItemPtr getRootItem();

    virtual void refresh();

    /*overwrite EItemVisitor's core function*/
    virtual bool handleEItem(EItemPtr item, int depth, EItemPtr parentItem);
    /*This selected item may be sync with UI's operation*/
    virtual EItemPtr getSelectedItem();
    /*Add item to another item, need to be rewrited by sub class.*/
    // virtual bool addItem(EItemPtr parent, long tagTypeItem);
    //convenient addItem, that do not need parent as input,.
    //This function calling getSelectedItem as parent.

    virtual EItemPtr addItem(EItemPtr typeItem);
    virtual bool deleteItem(EItemPtr item);
    virtual EItemPtr copyItem(EItemPtr item);

    /*select a item, need to be rewrited by sub class*/
    virtual void selectItem(EItemPtr item);
    virtual void disselectItem(EItemPtr item);

    virtual void handleCommand(const ECommandPtr& command);
    // virtual void addItem(EItemPtr item);
    // virtual void changeItem(EItemPtr item);
    // virtual void selectItemList(EItemPtr item);
    //
    //add to m_cmdSenderList
    void addCmdSenderList(long p);
    //check if the a cmd sender is in m_cmdSenderList
    //TODO: if p=null, then all event can be handled in this EController
    bool ifCmdSenderValid(long p);
protected:
    EItemPtr m_rootEItem;
    EItemPtr m_selectedEItem;
    
    //used by sub class for ECommand filtering
    EList<long> m_cmdSenderList;
};

#endif // EITEM_H
